package demointerface;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {//实现Comparable<类名>
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {//自我重写实现compareTo
        //return this.age - o.age;//通过年龄比较
        return this.name.compareTo(o.name);//姓名比较
    }
}

class AgeComparaTor implements Comparator<Student> {

    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class NameComparaTor implements Comparator<Student>{//使用方法实现Comparator
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {//自我实现判断

    public static void  mySort(Comparable[] comparables) {
        for(int i = 0;i < comparables.length - 1;i++) {
            for(int j = 0;j < comparables.length - i - 1;j++) {
                if(comparables[j].compareTo(comparables[j + 1]) > 0 ) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        //比较器 分别创建不同类的接口
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",15);
        AgeComparaTor ageComparaTor = new AgeComparaTor();
        System.out.println(ageComparaTor.compare(student1, student2));//为真打印打印大于0的数为假反之
        NameComparaTor nameComparaTor = new NameComparaTor();
        System.out.println(nameComparaTor.compare(student1, student2));//为真打印打印大于0的数为假反之
        System.out.println("---------------------");//进阶比较
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",15);
        students[1] = new Student("lisi",20);
        students[2] = new Student("wangwu",35);
        AgeComparaTor ageComparaTor1= new AgeComparaTor();
        Arrays.sort(students,ageComparaTor1);//可以传一个排序数组 和比较器
        System.out.println(Arrays.toString(students));//年龄比较器 排序打印
        NameComparaTor nameComparaTor1 = new NameComparaTor();
        Arrays.sort(students,nameComparaTor1);
        System.out.println(Arrays.toString(students));//姓名比较器 排序打印
    }
//    public static void main2(String[] args) {
//        //对象数组排列
//        Student[] students = new Student[3];
//        students[0] = new Student("zhangsan",15);
//        students[1] = new Student("lisi",20);
//        students[2] = new Student("wangwu",35);
//
//        //Arrays.sort(students);
//        mySort(students);
//
//        System.out.println(Arrays.toString(students));
//    }
//    public static void main1(String[] args) {
//        Student student1 = new Student("zhangsan",10);
//        Student student2 = new Student("lisi",15);
//
//        if(student1.compareTo(student2) > 0) {
//            System.out.println("student1 > student2");
//        }else {
//            System.out.println("student1 < student2");
//        }
//    }
}
